package kr.co.probrain.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import kr.co.probrain.util.DBManager;

public class MemberDAO {
	public int getMemberCountByName( String name ) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			DBManager dbm = new DBManager();
			conn = dbm.getConn();
			String sql = "SELECT COUNT(*) FROM T_MEMBER" +
						 " WHERE NAME = ?";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			
			ResultSet rs = pstmt.executeQuery();
			int result = 0;
			
			while( rs.next() ) {
				result = Integer.parseInt( rs.getString("COUNT(*)") );
			}
			return result;
		} catch (Exception e) {
		 	e.printStackTrace();
		} finally {
			try {
				if( pstmt != null ) pstmt.close();
				if( conn != null ) conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return 0;
	}
	
	public int getMemberIDByName( String name ) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			DBManager dbm = new DBManager();
			conn = dbm.getConn();
			String sql = "SELECT ID FROM T_MEMBER" +
						 " WHERE NAME = ?";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			
			ResultSet rs = pstmt.executeQuery();
			int result = 0;
			
			while( rs.next() ) {
				result = rs.getInt("ID");
			}
			return result;
		} catch (Exception e) {
		 	e.printStackTrace();
		} finally {
			try {
				if( pstmt != null ) pstmt.close();
				if( conn != null ) conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return 0;
	}
	
}
